package com.ruoyi.student.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.student.domain.Student;
import com.ruoyi.student.service.IStudentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 学生信息Controller
 * 
 * @author zyq
 * @date 2023-10-18
 */
@RestController
@RequestMapping("/user")
public class userStudentController extends BaseController {
    @Autowired
    private IStudentService studentService;

    /**
     * 获取学生信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(studentService.selectStudentById(id));
    }

    /**
     * 新增或修改学生信息
     */
    @PostMapping("/save")
    public AjaxResult addOrUpdate(@RequestBody Student studentC) {
        System.out.println("111111111111111111111111111111111111");
        Student student = new Student();
        BeanUtils.copyProperties(studentC,student);
        System.out.println("1111222222222222222222222222222222222222221111");

        student.setPwd("-1");
        AjaxResult ajax ;
        System.out.println("11111133333333333333333333333333333333331111111");

        if (student.getAccount() != null) {
            System.out.println("114444444444444444444444444444444444444444411111");

            Student existingStudent = studentService.selectStudentByAccount(student.getAccount());
            if (existingStudent != null) {
                System.out.println("114444444444455555555555555555555555555555555555555444411111");
                System.out.println(student);

                student.setId(existingStudent.getId());
                // 学生信息已存在，执行更新操作
                studentService.updateStudent(student);

                ajax = AjaxResult.success();
                ajax.put("student", student);
                return ajax;
            }
            // 学生信息不存在，执行新增操作
            studentService.insertStudent(student);
            ajax = AjaxResult.success();
            ajax.put("student", student);
            return ajax;
        }
        ajax = AjaxResult.error();
        return ajax;
    }
}
